package com.yhy.tcpserver.outbound;

import io.netty.buffer.ByteBuf;
import io.netty.channel.*;

/**
 * describe:
 *
 * @author helloworldyu
 * @data 2018/3/14
 */
public class FirstOutboundChannel extends ChannelOutboundHandlerAdapter{
	@Override
	public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
		System.out.println("===============firstoutbound===============");
		//===============获取 byte 发送=================
		String stringMsg = (String) msg;
		ByteBuf buf = ctx.alloc().buffer(((String) msg).getBytes().length);
		buf.writeBytes(stringMsg.getBytes());

		//传到下一级
		ChannelFuture channelFuture = ctx.write(buf);
		//等待发送成功的通知。
		channelFuture.addListener(
				(ChannelFutureListener) future -> System.out.println("future: FirstOutboundHandler1 收到 write() 成功的消息"));

		ctx.flush();
	}
}